﻿
Imports DTO
Imports BUS
Public Class frmLuuBoKichDien
    Private bkdController As BoKichDienBUS
    Private _bkd As BoKichDienDTO 
    Private _lbkd As LoaiBoKichDienDTO
    Private sListBoKichDien As New List(Of BoKichDienDTO)
    Private flagIsUp As Integer
    Private sListChonSua As New List(Of Integer)
    Private flagIsSuaNhieu As Boolean

    '------------------------------------------------------'
    '                 Các Hàm Xử Lý Phụ                     '
    '------------------------------------------------------'
    Private _trangThaiDangNhap As Boolean
    Public Sub TrangThai(trangthai As Boolean)
        _trangThaiDangNhap = trangthai
    End Sub
    Private Sub LoadText(ByVal i As Integer)
        lbIdSelect.Text = i
        lbIdBoKich.Text = dgvDuLieuThem.Rows(i).Cells(0).FormattedValue.ToString()
        txtTenBKD.Text = dgvDuLieuThem.Rows(i).Cells(1).FormattedValue.ToString()
        txtHangSX.Text = dgvDuLieuThem.Rows(i).Cells(2).FormattedValue.ToString()
        cbLoaiBKD.SelectedValue = dgvDuLieuThem.Rows(i).Cells(4).FormattedValue.ToString()
        txtDienApTai.Text = dgvDuLieuThem.Rows(i).Cells(5).FormattedValue.ToString()
        txtCongSuatTai.Text = dgvDuLieuThem.Rows(i).Cells(6).FormattedValue.ToString()
        txtGia.Text = dgvDuLieuThem.Rows(i).Cells(7).FormattedValue.ToString()
    End Sub

    Private Sub LockText(ByVal b As Boolean)
        txtTenBKD.ReadOnly = b
        cbLoaiBKD.Enabled = Not b
        txtHangSX.ReadOnly = b
        txtDienApTai.ReadOnly = b
        txtCongSuatTai.ReadOnly = b
        txtGia.ReadOnly = b
        If (b = True) Then
            txtTenBKD.ForeColor = Color.Black
            cbLoaiBKD.ForeColor = Color.Black
            txtHangSX.ForeColor = Color.Black
            txtDienApTai.ForeColor = Color.Black
            txtCongSuatTai.ForeColor = Color.Black
            txtGia.ForeColor = Color.Black
        End If
    End Sub

    Private Sub LockTextInput(ByVal b As Boolean)
        txtDiaChiFileExcel.ReadOnly = b
        btnInput.Enabled = Not b
        If b = True Then
            txtDiaChiFileExcel.ForeColor = Color.Black
        End If
    End Sub

    Private Sub BlockButton(ByVal t As Control, ByVal h As Control, ByVal v As Control, ByVal r As Control, ByVal s As Control, ByVal x As Control, Optional ByVal capdo As Byte = 0)
        Select Case capdo
            Case 0
                t.Enabled = True
                h.Enabled = True
                v.Enabled = True
                r.Enabled = True
                s.Enabled = True
                x.Enabled = True

            Case 1
                t.Enabled = True
                h.Enabled = False
                v.Enabled = False
                r.Enabled = False
                s.Enabled = False
                x.Enabled = False
        End Select
    End Sub

    Private Sub Reset()
        txtTenBKD.Text = ""
        txtHangSX.Text = ""
        txtCongSuatTai.Text = ""
        txtDienApTai.Text = ""
        txtGia.Text = ""
        cbLoaiBKD.SelectedIndex = -1
    End Sub

    Private Function TestData() As Boolean
        TestData = True
        If txtTenBKD.Text = "" Then
            TestData = False
            MsgBox("Tên pin không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTenBKD.Focus()
            Exit Function
            'ElseIf txtHangSX.Text = "" Then
            '   TestData = False
            '  MsgBox("Hãng sản xuất không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            ' txtHangSX.Focus()
            'Exit Function
        ElseIf cbLoaiBKD.SelectedIndex = -1 Then
            TestData = False
            MsgBox("Loại pin không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            cbLoaiBKD.Focus()
            Exit Function
        ElseIf txtDienApTai.Text = "" Then
            TestData = False
            MsgBox("Dòng điện không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtDienApTai.Focus()
            Exit Function
            'ElseIf txtCongSuatTai.Text = "" Then
            '   TestData = False
            '  MsgBox("Điện áp không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            ' txtCongSuatTai.Focus()
            'Exit Function
        ElseIf txtGia.Text = "" Then
            TestData = False
            MsgBox("Giá không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtGia.Focus()
            Exit Function
        End If

    End Function

    Private Function SelectNameLoai(ByVal idLoai As Integer)
        Dim sListName As List(Of LoaiBoKichDienDTO) = bkdController.SelectNameLoai(idLoai)
        Return sListName(0).TenLoai
    End Function

    Private Sub LoadGrid()
        bkdController = New BoKichDienBUS()
        dgvDuLieuThem.Rows.Clear()
        For i As Integer = 0 To sListBoKichDien.Count - 1
            dgvDuLieuThem.Rows.Add()
            dgvDuLieuThem.Rows.Item(i).Cells(0).Value = sListBoKichDien(i).MaBoKichDien
            dgvDuLieuThem.Rows.Item(i).Cells(1).Value = sListBoKichDien(i).TenBoKichDien
            dgvDuLieuThem.Rows.Item(i).Cells(2).Value = sListBoKichDien(i).HangSanXuat
            dgvDuLieuThem.Rows.Item(i).Cells(3).Value = SelectNameLoai(Val(sListBoKichDien(i).MaLoai))
            dgvDuLieuThem.Rows.Item(i).Cells(4).Value = sListBoKichDien(i).MaLoai
            dgvDuLieuThem.Rows.Item(i).Cells(5).Value = sListBoKichDien(i).DienApTai
            dgvDuLieuThem.Rows.Item(i).Cells(6).Value = sListBoKichDien(i).CongSuatTai
            dgvDuLieuThem.Rows.Item(i).Cells(7).Value = sListBoKichDien(i).Gia
        Next
    End Sub

    '------------------------------------------------------'
    '                 Phần xử lý chính                    '
    '------------------------------------------------------'
    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub
    Public Sub AddList(ByVal sListSelect As IList(Of Integer))
        For i As Integer = 0 To sListSelect.Count - 1
            bkdController = New BoKichDienBUS()
            Dim j As Integer = Val(sListSelect.Item(i))
            sListBoKichDien.AddRange(bkdController.SelectById(j))
        Next
        flagIsUp = 2
    End Sub


    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        flagIsUp = 1
        Dim MaBoKichDien As Integer = 0
        Dim TenBKD As String = txtTenBKD.Text
        Dim MLoai As Integer = Val(cbLoaiBKD.SelectedValue)
        Dim TMLoai As String = cbLoaiBKD.SelectedText
        Dim HSX As String = txtHangSX.Text
        Dim CongSuatTai As Single = Val(txtCongSuatTai.Text)
        Dim DienApTai As Single = Val(txtDienApTai.Text)
        Dim Gia As Integer = Val(txtGia.Text)
        If TestData() = True Then
            _bkd = New BoKichDienDTO(MaBoKichDien, TenBKD, MLoai, HSX, DienApTai, CongSuatTai, Gia)
            sListBoKichDien.Add(_bkd)
            LoadGrid()
        End If
        BlockButton(btnThem, btnUpdate, btnNhapLai, btnXoaMot, btnXoaHet, btnSave, 0)
    End Sub

    Private Sub frmLuuBoKichDien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim mdiparent As Main = DirectCast(Me.MdiParent, Main)
        If mdiparent.DaDangNhap = False Then
            If MessageBox.Show("Bạn chưa nhập Mật Khẩu để thực hiện chỉnh sửa Cơ Sở Dữ Liệu. Bạn có muốn nhập Mật Khẩu không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                Dim frmMatKhau As New NhapMatKhau()
                'frmMatKhau.MdiParent = Me.MdiParent
                Dim result = frmMatKhau.ShowDialog()
                If result = Windows.Forms.DialogResult.Cancel Then
                    Me.Close()
                ElseIf result = Windows.Forms.DialogResult.OK Then

                    mdiparent.DangNhap()
                End If
            Else
                Me.Close()
            End If
        End If
        cbLoaiBKD.Items.Clear()
        Dim dt As New DataTable
        bkdController = New BoKichDienBUS()
        Dim sListlaq As New List(Of LoaiBoKichDienDTO)
        'bkdController.LoadComboListbox(cbLoaiBKD, "Select * From LOAIACQUY", "MaLoai")
        dt = bkdController.LoadComboListbox()
        If dt.Rows.Count > 0 Then
            cbLoaiBKD.DataSource = dt
            cbLoaiBKD.ValueMember = "MaLoai"
            cbLoaiBKD.DisplayMember = "TenLoai"
        End If
        cbLoaiBKD.SelectedIndex = -1
        BlockButton(btnThem, btnUpdate, btnNhapLai, btnXoaMot, btnXoaHet, btnSave, 1)
        LockTextInput(True)
        If sListBoKichDien.Count > 0 Then
            LoadGrid()
            BlockButton(btnThem, btnUpdate, btnNhapLai, btnXoaMot, btnXoaHet, btnSave, 0)
        End If

    End Sub
    Private Sub cbInputFromFile_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cbInputFromFile.CheckedChanged
        If cbInputFromFile.Checked = True Then
            LockText(True)
            LockTextInput(False)
        Else
            LockText(False)
            LockTextInput(True)
        End If
    End Sub
    Private Sub dgvDuLieuThem_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDuLieuThem.CellContentClick
        'Dim flag = 0
        'Dim j As Integer = dgvDuLieuThem.Rows(e.RowIndex).Cells(0).Value
        'For i As Integer = 0 To sListChonSua.Count - 1
        '    If j = Val(sListChonSua(i)) Then
        '        sListChonSua.RemoveAt(i)
        '        flag = 1
        '        Exit For
        '    End If
        'Next
        'If flag = 0 Then
        '    sListChonSua.Add(j)
        'End If
        'If dgvDuLieuThem.SelectedCells.Count() = 1 Then
        '    LoadText(e.RowIndex)
        '    flagIsSuaNhieu = False
        'Else
        '    flagIsSuaNhieu = True
        '    txtTenBKD.Text = ""
        '    txtTenBKD.ReadOnly = True
        '    Reset()
        'End If
    End Sub

    Private Sub btnNhapLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapLai.Click
        Reset()
    End Sub

    Private Sub btnXoaHet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaHet.Click
        sListBoKichDien.Clear()
        LoadGrid()
    End Sub

    Private Sub btnXoaMot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaMot.Click
        For i As Integer = 0 To sListBoKichDien.Count - 1
            If sListBoKichDien(i).TenBoKichDien = txtTenBKD.Text And sListBoKichDien(i).MaLoai = cbLoaiBKD.SelectedValue Then
                sListBoKichDien.RemoveAt(i)
                Exit For
            End If
        Next
        LoadGrid()
    End Sub


    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (sListBoKichDien.Count > 0) Then
            For i As Integer = 0 To sListBoKichDien.Count - 1
                Dim listIsUp As New BoKichDienDTO(sListBoKichDien(i).MaBoKichDien, sListBoKichDien(i).TenBoKichDien, sListBoKichDien(i).MaLoai, sListBoKichDien(i).HangSanXuat, sListBoKichDien(i).DienApTai, sListBoKichDien(i).CongSuatTai, sListBoKichDien(i).Gia)
                bkdController.InsertBoKichDien(listIsUp, flagIsUp)
            Next
            MsgBox("Đã xử lý dữ liệu thành công", MsgBoxStyle.Information, "Thông báo")
            sListBoKichDien.Clear()
            LoadGrid()
            BlockButton(btnThem, btnUpdate, btnNhapLai, btnXoaMot, btnXoaHet, btnSave, 1)
            Reset()
            btnThoat_Click(sender, e)
        End If
    End Sub
    Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click
        ofPin.Filter = "Excel (*.xls;*.xlsx)|*.xls;*.xlsx"
        ofPin.ShowDialog()

        txtDiaChiFileExcel.Text = ofPin.FileName
        Dim MyConnection As System.Data.OleDb.OleDbConnection
        Dim DtSet As System.Data.DataSet
        Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
        MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & txtDiaChiFileExcel.Text & "';Extended Properties=Excel 8.0;")
        MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
        MyCommand.TableMappings.Add("Table", "Net-informations.com")
        DtSet = New System.Data.DataSet
        MyCommand.Fill(DtSet)
        For i As Integer = 0 To DtSet.Tables(0).Rows.Count() - 1
            Dim MaBoKichDien As Integer = 0
            Dim TenBKD As String = DtSet.Tables(0).Rows(i).Item(0).ToString()
            Dim MLoai As Integer = Val(DtSet.Tables(0).Rows(i).Item(1))
            Dim HSX As String = DtSet.Tables(0).Rows(i).Item(2).ToString()
            Dim CongSuatTai As Single = Val(DtSet.Tables(0).Rows(i).Item(3))
            Dim DienApTai As Single = Val(DtSet.Tables(0).Rows(i).Item(4))
            Dim Gia As Integer = Val(DtSet.Tables(0).Rows(i).Item(5))
            _bkd = New BoKichDienDTO(MaBoKichDien, TenBKD, MLoai, HSX, CongSuatTai, DienApTai, Gia)
            sListBoKichDien.Add(_bkd)
        Next
        LoadGrid()
        MyConnection.Close()
        flagIsUp = 1
        BlockButton(btnThem, btnUpdate, btnNhapLai, btnXoaMot, btnXoaHet, btnSave, 0)
        LockText(False)
    End Sub


    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        If dgvDuLieuThem.SelectedRows.Count = 1 Then
            For i = 0 To dgvDuLieuThem.SelectedRows.Count - 1
                Dim a As Integer = Integer.Parse(lbIdSelect.Text)

                If sListBoKichDien(a).MaBoKichDien = Val(lbIdBoKich.Text) Then
                    sListBoKichDien(a).TenBoKichDien = txtTenBKD.Text
                    sListBoKichDien(a).HangSanXuat = txtHangSX.Text
                    sListBoKichDien(a).MaLoai = cbLoaiBKD.SelectedValue
                    sListBoKichDien(a).DienApTai = Val(txtDienApTai.Text)
                    sListBoKichDien(a).CongSuatTai = Val(txtCongSuatTai.Text)
                    sListBoKichDien(a).Gia = Val(txtGia.Text)
                End If
            Next
        ElseIf dgvDuLieuThem.SelectedRows.Count > 1 Then
            For i = 0 To dgvDuLieuThem.SelectedRows.Count - 1
                sListChonSua.Add(Integer.Parse(dgvDuLieuThem.SelectedRows(i).Cells("MaBoKichDien").Value.ToString()))
            Next
            For j As Integer = 0 To sListChonSua.Count - 1
                For i As Integer = 0 To sListBoKichDien.Count - 1
                    If sListChonSua(j) = sListBoKichDien(i).MaBoKichDien Then
                        If txtHangSX.Text <> "" Then
                            sListBoKichDien(i).HangSanXuat = txtHangSX.Text
                        End If
                        If cbLoaiBKD.SelectedIndex <> -1 Then
                            sListBoKichDien(i).MaLoai = cbLoaiBKD.SelectedValue
                        End If
                        If txtDienApTai.Text <> "" Then
                            sListBoKichDien(i).DienApTai = Val(txtDienApTai.Text)
                        End If
                        If txtCongSuatTai.Text <> "" Then
                            sListBoKichDien(i).CongSuatTai = Val(txtCongSuatTai.Text)
                        End If
                        If txtGia.Text <> "" Then
                            sListBoKichDien(i).Gia = Val(txtGia.Text)
                        End If
                    End If
                Next
            Next
            sListChonSua.Clear()
        End If
        LoadGrid()
    End Sub

    Private Sub dgvDuLieuThem_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDuLieuThem.CellClick
        If dgvDuLieuThem.SelectedRows.Count = 1 Then
            LoadText(dgvDuLieuThem.SelectedRows(0).Index)
        ElseIf dgvDuLieuThem.SelectedRows.Count > 1 Then
            txtTenBKD.Text = ""
            txtTenBKD.ReadOnly = True
            Reset()
        End If
    End Sub



    Private Sub txtCongSuatTai_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCongSuatTai.TextChanged

    End Sub
End Class